<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_probeview']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}
include "languages.php";
include_once "functions.inc.php";
include "spioparser.inc.php";
require "config.php";
include "attributes.php";

$userid = $_SESSION['s_userid'];
$ogameserver = $_SESSION['s_ogameserver'];

if($ogameserver == 'ogame.com.cn') {
	require "../languages/chinese_probes.inc.php";
} else if($ogameserver == 'ogame.org') {
	require "../languages/english_probes.inc.php";
} else if($ogameserver == 'ogame.tw') {
	require "../languages/taiwanese_probes.inc.php";
}

$db_array1 = array_flip($db_array);

mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
mysql_select_db($dbname) or die(PARSER_ERROR2);

makeheader(REPORTS_TITLE);

if (isset($_GET['delete']) && strlen($_GET['delete']) < 10 && $_SESSION['s_caninsert']) {
    $query = "DELETE FROM $reporttable WHERE userid=$userid AND coordinates='".mysql_real_escape_string($_GET['delete'])."'";
    if (isset($_GET['moon']) && $_GET['moon'] == "true") {
        $query .= " AND moon='true'";
    } else {
        $query .= " AND moon='false'";
    }
    $res = mysql_query($query) or die(mysql_error());
    $query = "SELECT count(*) as anzahl FROM $reporttable WHERE userid=$userid AND coordinates='".mysql_real_escape_string($_GET['delete'])."'";
    $res = mysql_query($query) or die(mysql_error());
    $line = mysql_fetch_object($res);
    $anzahl = $line->anzahl;
    if ($anzahl == 0) {
    	// Delete setting in galaxy table
    	$query = "UPDATE $dbtablename SET reports='false' WHERE userid=$userid AND coordinates='".mysql_real_escape_string($_GET['delete'])."'";
    	$res = mysql_query($query) or die(mysql_error());
    }
    if ($res !== false) echo "<div align=\"center\">".REPORTS_DELETED."</div>";
}


if (isset($_GET['action']) && $_GET['action'] == "search") {
    unset($_SESSION['s_playername']);
    unset($_SESSION['s_allyname']);
    
    echo '
        <form name="reports" method="post" action="reports.php?action=searchres">
        <table cellpadding="1" cellspacing="0" border="0" align="center" width="85%">
        <tr class="tblhead"><td colspan="4">'.REPORTS_SEARCH.'</td></tr>
        <tr class="tblhead"><td colspan="4">'.REPORTS_RESOURCES.'</td></tr>
        ';
    $i = 0;
    foreach ($db_array1 as $key => $value) {
        if ($key != "id" && $key != "coordinates" && $key != "moon" && $key != "scantime") {
            if ($key == "kt" || $key == "rak" || $key == "memi" || $key == "spiolvl") {
                if ($i % 2 != 0) echo "<td colspan=\"2\">&nbsp;</td></tr>";
                echo "<tr class=\"tblhead\"><td colspan=\"4\">";
                switch ($key) {
                    case "kt" : echo REPORTS_FLEET; break;
                    case "rak" : echo REPORTS_DEFENCE; break;
                    case "memi" : echo REPORTS_BUILDINGS; break;
                    case "spiolvl" : echo REPORTS_TECHNOLOGY; break;
                }
                echo "</td></tr>";
                $i = 0;
            }
            if ($i % 2 == 0) echo "<tr class=\"firstcolor\">";
            echo "<td style=\"padding-left:20px;\">".$db_array1[$key]."</td><td><input class=\"textfield\" name=\"$key\" value=\"0\" style=\"width:50px; text-align:right;\" maxlength=\"15\"></td>";
            $i++;
            if ($i % 2 == 0) echo "</tr>";
        }
    }
    if (!isset($_SESSION['s_hits'])) $_SESSION['s_hits'] = 20;
    if ($i % 2 != 0) echo "<td colspan=\"2\">&nbsp;</td></tr>";
    echo '<tr class="firstcolor"><td colspan="4" style="padding-left:20px;">'.REPORTS_SEARCH_IN.'&nbsp;&nbsp;&nbsp;<input class="textfield" name="galaxie" type="text" size="3" maxlength="2">&nbsp;&nbsp;&nbsp;'.REPORTS_FROM_SYSTEM.'&nbsp;&nbsp;&nbsp;<input class="textfield" name="system1" type="text" size="5" maxlength="3">&nbsp;&nbsp;&nbsp;'.REPORTS_TO.'&nbsp;&nbsp;&nbsp;<input class="textfield" name="system2" type="text" size="5" maxlength="3"></td></tr>';
    ?><tr class="firstcolor"><td style="padding-left:20px;"><?php echo REPORTS_RESULTS; ?></td>
    <td colspan="3" align="left"><select name="entries" class="pulldown_code">
    <option value="10" <?php if ($_SESSION['s_hits'] == 10) echo "selected"; ?>>10</option>
    <option value="20" <?php if ($_SESSION['s_hits'] == 20) echo "selected"; ?>>20</option>
    <option value="50" <?php if ($_SESSION['s_hits'] == 50) echo "selected"; ?>>50</option>
    <option value="100" <?php if ($_SESSION['s_hits'] == 100) echo "selected"; ?>>100</option>
    </select>
    </td></tr>
    <tr class="firstcolor"><td style="padding-left:20px;"><?php echo REPORTS_PLAYERNAME; ?></td>
    <td colspan="3" align="left"><input class="textfield" name="playername" value="<?php echo (isset($_SESSION['s_playername'])) ? $_SESSION['s_playername'] : ""; ?>" />
    </td></tr>
    <tr class="firstcolor"><td style="padding-left:20px;"><?php echo REPORTS_ALLY; ?></td>
    <td colspan="3" align="left"><input class="textfield" name="allyname" value="<?php echo (isset($_SESSION['s_allyname'])) ? $_SESSION['s_allyname'] : ""; ?>" />
    </td></tr>    
    <tr class="firstcolor">
    <td colspan="4" style="padding-left:20px;">
        <table cellpadding="0" cellspacing="0" border="0">
        <tr class="firstcolor"><td rowspan="3" style="width:100px;"><?php echo REPORTS_DATE; ?>:</td>
        <td style="width:50px;"><?php echo REPORTS_DATE_1; ?></td>
        <td>
        <?php
        // Monat
        pulldowncode("month1",1,12,1, date("m"));
        echo "-";
        // Tag
        pulldowncode("day1",1,31,1, date("d"));
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        // Stunde
        pulldowncode("hour1",0,23,1, date("H"));
        echo ":";
        // Minute
        pulldowncode("minute1",0,59,1, date("i"));
        echo ":";
        // Sekunde
        pulldowncode("second1",0,59,1, date("s"));
        echo "&nbsp;";
        ?>
        </td>
        </tr>
        <tr class="firstcolor">
        <td><?php echo REPORTS_DATE_2; ?></td>
        <td>
        <?php
        // Monat
        pulldowncode("month2",1,12,1, date("m"));
        echo "-";
        // Tag
        pulldowncode("day2",1,31,1, date("d"));
        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        // Stunde
        pulldowncode("hour2",0,23,1, date("H"));
        echo ":";
        // Minute
        pulldowncode("minute2",0,59,1, date("i"));
        echo ":";
        // Sekunde
        pulldowncode("second2",0,59,1, date("s"));
        echo "&nbsp;";
        ?>
        </td>
        </tr>
        <tr class="firstcolor">
        <td colspan="2"><input class="textfield" type="checkbox" name="datevalue" value="1" checked /><?php echo REPORTS_DATE_0; ?></td>
        </tr>
        </table>
    </td></tr>    
    <?php
    echo '<tr class="firstcolor"><td colspan="4" align="center" style="padding-top:10px; padding-bottom:10px;"><input class="button" type="submit" name="'.REPORTS_SEARCH_SHORT.'" value="'.REPORTS_SEARCH_SHORT.'" /></td></tr>';
    echo "</table></form>\n";
}


if (isset($_GET['action']) && $_GET['action']=="searchres") {

    $page = (isset($_GET['page'])) ? $_GET['page'] : 1;
    $entries_per_page = (isset($_GET['entries'])) ? $_GET['entries'] : 20;
    $entries_per_page = (isset($_POST['entries'])) ? $_POST['entries'] : 20;
    
    if (isset($_POST[REPORTS_SEARCH_SHORT])) {
        $searcharray = $_POST;
        $galaxie     = (int)$searcharray["galaxie"];
        $system1     = (int)$searcharray["system1"];
        $system2     = (int)$searcharray["system2"];
        $datevalue   = (isset($searcharray["datevalue"])) ? false : true;
        $day1        = add_missing_null((int)$searcharray["day1"]);
        $month1      = add_missing_null((int)$searcharray["month1"]);
        $hour1       = add_missing_null((int)$searcharray["hour1"]);
        $minute1     = add_missing_null((int)$searcharray["minute1"]);
        $second1     = add_missing_null((int)$searcharray["second1"]);
        $day2        = add_missing_null((int)$searcharray["day2"]);
        $month2      = add_missing_null((int)$searcharray["month2"]);
        $hour2       = add_missing_null((int)$searcharray["hour2"]);
        $minute2     = add_missing_null((int)$searcharray["minute2"]);
        $second2     = add_missing_null((int)$searcharray["second2"]);
        $time1       = "$month1-$day1 $hour1:$minute1:$second1";
        $time2       = "$month2-$day2 $hour2:$minute2:$second2";
    	$playername  = trim(strip_tags($searcharray["playername"]));
    	$playername  = preg_replace("/[\'\"?%\\\\]/","", $playername);        
    	$allyname    = trim(addslashes(strip_tags($searcharray["allyname"])));
    	$allyname    = preg_replace("/[^a-zA-Z0-9\*\s-_.]/","", $allyname);        
        
        unset($searcharray[REPORTS_SEARCH_SHORT]);
        unset($searcharray["galaxie"]);
        unset($searcharray["system1"]);
        unset($searcharray["system2"]);
        unset($searcharray["entries"]);
        if (isset($searcharray["datevalue"])) unset($searcharray["datevalue"]);
        unset($searcharray["day1"]);
        unset($searcharray["month1"]);
        unset($searcharray["hour1"]);
        unset($searcharray["minute1"]);
        unset($searcharray["second1"]);
        unset($searcharray["day2"]);
        unset($searcharray["month2"]);
        unset($searcharray["hour2"]);
        unset($searcharray["minute2"]);
        unset($searcharray["second2"]);
        unset($searcharray["playername"]);
        unset($searcharray["allyname"]);
        
        if (isset($_SESSION['s_spioarray']) || isset($_SESSION['s_spiogala']) || isset($_SESSION['s_spiosystem1']) || isset($_SESSION['s_spiosystem2']) ||isset($_SESSION['s_hits'])) {
            unset($_SESSION['s_spioarray']);
            unset($_SESSION['s_spiogala']);
            unset($_SESSION['s_spiosystem1']);
            unset($_SESSION['s_spiosystem2']);
            unset($_SESSION['s_hits']);
            unset($_SESSION['s_datevalue']);
            unset($_SESSION['s_time1']);
            unset($_SESSION['s_time2']);
        }
        $_SESSION['s_spioarray']   = $searcharray;
        $_SESSION['s_spiogala']    = $galaxie;
        $_SESSION['s_spiosystem1'] = $system1;
        $_SESSION['s_spiosystem2'] = $system2;
        $_SESSION['s_hits']        = $entries_per_page;
        $_SESSION['s_playername']  = $playername;
        $_SESSION['s_allyname']    = $allyname;
        if ($datevalue) {
            $_SESSION['s_datevalue'] = true;
            $_SESSION['s_time1'] = "$month1-$day1 $hour1:$minute1:$second1";
            $_SESSION['s_time2'] = "$month2-$day2 $hour2:$minute2:$second2";
        }
        
    } else {
        if (isset($_SESSION['s_spioarray'])) {
            $searcharray = $_SESSION['s_spioarray'];
        }
        if (isset($_SESSION['s_spiogala'])) {
            $galaxie = $_SESSION['s_spiogala'];
        }
        if (isset($_SESSION['s_spiosystem1'])) {
            $system1 = $_SESSION['s_spiosystem1'];
        }
        if (isset($_SESSION['s_spiosystem2'])) {
            $system2 = $_SESSION['s_spiosystem2'];
        }
        if (isset($_SESSION['s_hits'])) {
            $entries_per_page = $_SESSION['s_hits'];
        }
        if (isset($_SESSION['s_datevalue'])) {
            $datevalue = $_SESSION['s_datevalue'];
        } else {
            $datevalue = false;
        }
        if (isset($_SESSION['s_time1'])) {
            $time1 = $_SESSION['s_time1'];
        }
        if (isset($_SESSION['s_time2'])) {
            $time2 = $_SESSION['s_time2'];
        }
        if (isset($_SESSION['s_playername'])) {
            $playername = $_SESSION['s_playername'];
        }
        if (isset($_SESSION['s_allyname'])) {
            $allyname = $_SESSION['s_allyname'];
        }        
    }
    $query  = "SELECT count(*) as anzahl
               FROM $reporttable r , $dbtablename u 
               WHERE r.userid=$userid AND r.userid=u.userid ";
    if ($galaxie != 0) $query .= " AND u.galaxie='$galaxie' ";
    if ($system1 != 0 && $system2 != 0) $query .= " AND (u.system BETWEEN $system1 AND $system2) ";
    $query .= " AND (u.coordinates = r.coordinates) AND ";
    if ($datevalue) {
        // check times
        if ($time2 >= $time1) {
            $query .= " scantime > '$time1' AND scantime < '$time2' AND ";
        } else {
            // $time1 > $time2 - d.h. zb von Okt bis Feb schauen
            $query .= " ((scantime > '$time1' AND scantime < '12-31 23:59:59') OR
                         (scantime < '$time2' AND scantime > '01-01 00:00:00')) AND ";
        }
    }
    if (isset($playername) && !empty($playername)) $query .= " u.spielername='$playername' AND ";
    if (isset($allyname) && !empty($allyname)) $query .= " u.ally='$allyname' AND ";
    
    $limit = " LIMIT ".(($page-1)*$entries_per_page).",".$entries_per_page;
    foreach ($searcharray as $key => $value) {
        if ($value > 0) $query .= $key.">='$value' AND ";
        elseif ($value < 0) {
            $value *= -1;
            $query .= $key."<='$value' AND ";
        }
    }
    $results = 0;
    $anzahl = 0;

    // get results
    $query = substr($query,0,strlen($query)-4);
    $query .= " ORDER BY r.coordinates ";

    $res = mysql_query($query) or die(mysql_error());
    $line = mysql_fetch_object($res);
    $anzahl = $line->anzahl;
    $query = str_replace("count(*) as anzahl","*",$query).$limit;

    $res = mysql_query($query) or die(mysql_error());
    $results = mysql_num_rows($res);
    $to = ($page*$entries_per_page < $anzahl) ? $page*$entries_per_page : $anzahl;
    echo '
        <table cellpadding="0" cellspacing="2" border="0" align="center" width="85%">
        <tr class="tblhead"><td colspan="6">
        '.REPORTS_HITS.': '.(($page-1)*$entries_per_page+1).'-'.$to.' '.REPORTS_OF.' '.$anzahl.'<br />
        <tr class="tblhead"><td>'.REPORTS_ADDRESS.'</td><td>'.REPORTS_CURRENTNESS.'</td><td>'.REPORTS_PLAYERNAME.'</td><td>'.REPORTS_ALLY.'</td><td>'.REPORTS_VIEW.'</td><td>'.REPORTS_NOTICES.'</td></tr>
        ';
    while ($results > 0 && $line = mysql_fetch_object($res)) {
        $coordinates_array = explode(":",$line->coordinates);
        if ($line->moon == "true") {
            echo "<tr class=\"firstcolor\">
            <td align=\"center\"><a class=\"resultlink\" href=\"view.php?gala=".$coordinates_array[0]."&amp;system=".$coordinates_array[1]."\">$line->coordinates ".REPORTS_MOON."</a></td>
            <td align=\"center\">$line->scantime</td>
            <td align=\"center\"><a class=\"resultlink\" href=\"show.php?spieler=$line->spielername&new=true\">$line->spielername</a></td>
            <td align=\"center\"><a class=\"resultlink\" href=\"show.php?ally=$line->ally&new=true\">$line->ally</a></td>
            <td align=\"center\"><a href=\"reports.php?coordinates=$line->coordinates&amp;moon=1\"><img src=\"../images/spio.gif\" border=\"0\"></a></td>";
            if ($line->notices == "true") {
            	echo '<td align="center"><a href="notices.php?coordinates='.$line->coordinates.'&amp;action=view"><img src="../images/notice.gif" border="0"></a></td>';
            } else {
            	echo '<td align="center"><a href="notices.php?coordinates='.$line->coordinates.'&amp;action=view"><img src="../images/no_notice.gif" border="0"></a></td>';
            }
            echo "</tr>";
            
           
        } else {
            echo "<tr class=\"firstcolor\">
            <td align=\"center\"><a class=\"resultlink\" href=\"view.php?gala=".$coordinates_array[0]."&amp;system=".$coordinates_array[1]."\">$line->coordinates</a></td>
            <td align=\"center\">$line->scantime</td>
            <td align=\"center\"><a class=\"resultlink\" href=\"show.php?spieler=$line->spielername&new=true\">$line->spielername</a></td>
            <td align=\"center\"><a class=\"resultlink\" href=\"show.php?ally=$line->ally&new=true\">$line->ally</a></td>
            <td align=\"center\"><a href=\"reports.php?coordinates=$line->coordinates\"><img src=\"../images/spio.gif\" border=\"0\"></a></td>";
            if ($line->notices == "true") {
            	echo '<td align="center"><a href="notices.php?coordinates='.$line->coordinates.'&amp;action=view"><img src="../images/notice.gif" border="0"></a></td>';
            } else {
            	echo '<td align="center"><a href="notices.php?coordinates='.$line->coordinates.'&amp;action=view"><img src="../images/no_notice.gif" border="0"></a></td>';
            }
            echo "</tr>";
        }
        
    }
    if ($results == 0) {
        echo "<tr class=\"firstcolor\"><td align=\"center\" colspan=\"6\">".REPORTS_NOTHING."</td></tr>";
    }
    if ($anzahl > $entries_per_page) {
    	echo "<tr class=\"firstcolor\"><td colspan=\"6\" align=\"center\">";
    	draw_pagenumbers($page,$anzahl,$entries_per_page,$_SERVER['PHP_SELF']."?action=searchres","page");
    	echo "</td></tr>";    
    }
    echo "</table>\n";
     
     
}


if (isset($_GET['coordinates'])) {
    $coordinates_array = explode(":",$_GET['coordinates']);
    if (count($coordinates_array) == 3) {
        if (isset($_GET['moon']) && $_GET['moon'] == 1) {
            // Spiobericht vom Mond   
            $query = "SELECT * FROM $reporttable WHERE userid=$userid AND coordinates='".(int)$coordinates_array[0].":".(int)$coordinates_array[1].":".(int)$coordinates_array[2]."' AND moon='true'";
        } elseif (isset($_GET['moon']) && $_GET['moon'] == "unknown") {
            // Beide Spioberichte anzeigen (sofern 2 vorhanden sind)
            $query = "SELECT * FROM $reporttable WHERE userid=$userid AND coordinates='".(int)$coordinates_array[0].":".(int)$coordinates_array[1].":".(int)$coordinates_array[2]."' ORDER BY moon DESC";
        } else {
            // Spiobericht vom Planeten
            $query = "SELECT * FROM $reporttable WHERE userid=$userid AND coordinates='".(int)$coordinates_array[0].":".(int)$coordinates_array[1].":".(int)$coordinates_array[2]."' AND moon='false'";
        }
        
     function printReport($query) {
     	global $db_array1;
	global $top_array;
        $res = mysql_query($query) or die(mysql_error());
        if (mysql_num_rows($res) > 0) {
            $reports = 0;
            while ($line = mysql_fetch_assoc($res)) {
                
                if ($reports > 0) echo "<br /><br />";
                $moon = ($line["moon"] == "true") ? REPORTS_MOON : VIEW_PLANET;
                echo '
                <table cellpadding="0" cellspacing="0" border="0" align="center" width="85%">';
                foreach ($top_array as $key => $value) {
                	break; // just get the first entry (Resources on)
                }
                echo '<tr class="tblhead"><td colspan="4">'.$key.' '.$moon." [".$line['coordinates'].'] '.$report_when.' '.$line['scantime'].' (ec:'.$line['ec'].')'.'</td></tr>
                <tr class="tblhead"><td colspan="4" class="truespio">'.REPORTS_RESOURCES.'</td></tr>
                ';
                $i = 0;
                switch ($line['scanned']) {
                    case "ressources" : $scanned = 1; break;
                    case "fleet"      : $scanned = 2; break;
                    case "defence"    : $scanned = 3; break;
                    case "buildings"  : $scanned = 4; break;
                    case "research" : $scanned = 5; break;
                    default : $scanned = 1;
                }

                foreach ($line as $key => $value) {
                    if ($key != "id" && $key != "coordinates" && $key != "moon" && $key != "scantime" && $key != "scanned" && $key != 'totalships' && $key != 'ec' && $key != 'userid') {
                        if ($key == "kt" || $key == "rak" || $key == "memi" || $key == "spiolvl") {
                            if ($i % 2 != 0) echo "<td colspan=\"2\">&nbsp;</td></tr>";
                            echo "<tr class=\"tblhead\"><td colspan=\"4\"";
                            switch ($key) {
                                case "kt" : echo " class=\"";
                                            echo ($scanned>=2) ? "truespio" : "falsespio";
                                            echo "\">".REPORTS_FLEET; 
                                            echo ' <span style="color:white;">' . ($scanned>=2 ? '(' . $line['totalships'] . ')' : '') . '</span>';
                                            break;
                                case "rak" :  echo " class=\"";
                                            echo ($scanned>=3) ? "truespio" : "falsespio";
                                            echo "\">".REPORTS_DEFENCE; 
                                            break;
                                case "memi" :  echo " class=\"";
                                            echo ($scanned>=4) ? "truespio" : "falsespio";
                                            echo "\">".REPORTS_BUILDINGS; 
                                            break;
                                case "spiolvl" :  echo " class=\"";
                                            echo ($scanned>=5) ? "truespio" : "falsespio";
                                            echo "\">".REPORTS_TECHNOLOGY; 
                                            break;
                            }
                            echo "</td></tr>";
                            $i = 0;
                        }
                        if ($i % 2 == 0) echo "<tr class=\"firstcolor\">";
                        if ($key == 'metal' || $key == 'crystal' || $key == 'deuterium' || $key == 'energy') {
	                        if (isset($tmp) && is_array($tmp))
	                            echo "<td style=\"padding-left:20px;\">".$tmp[$db_array1[$key]].":</td><td>".number_format($value,0,",",".")."</td>";
	                        else 
	                            echo "<td style=\"padding-left:20px;\">".$db_array1[$key].":</td><td>".number_format($value,0,",",".")."</td>";
	                        $i++;
                        } elseif ($value > 0) {
	                        if (isset($tmp) && is_array($tmp))
	                            echo "<td style=\"padding-left:20px;\">".$tmp[$db_array1[$key]]."</td><td>".number_format($value,0,",",".")."</td>";
	                        else 
	                            echo "<td style=\"padding-left:20px;\">".$db_array1[$key]."</td><td>".number_format($value,0,",",".")."</td>";
							$i++;
                        }
                        
                        if ($i % 2 == 0) echo "</tr>";
                    }
                }
                if ($i % 2 != 0) echo "<td colspan=\"2\">&nbsp;</td></tr>";
                if($_SESSION['s_caninsert']) {
	                if ($line['moon'] == "true") {
	                    echo "<td  class=\"tblhead\" colspan=\"4\"><a class=\"resultlink\" href=\"reports.php?delete=".$_GET['coordinates']."&amp;moon=true\">".REPORTS_DELETE."</a></td></tr>";
	                } else {
	                    echo "<td  class=\"tblhead\" colspan=\"4\"><a class=\"resultlink\" href=\"reports.php?delete=".$_GET['coordinates']."\">".REPORTS_DELETE."</a></td></tr>";
	                }
        	}
                echo "</table>\n";
                $reports++;
            }
        }
      }
      
      printReport($query);
      echo '<table cellpadding="0" cellspacing="0" border="0" align="center" width="85%">
      <tr height="1"><td bgcolor="white"></td></tr>
      <tr height="30" class="tblhead"><td align="center" valign="center">======== '.REPORTS_PREVIOUS.' ========</td></tr>
      <tr height="1"><td bgcolor="white"></td></tr>
      </table>';
      $query_chg = str_replace($reporttable, $reporttable."_chg", $query);
      printReport($query_chg);
    }
    
}
makefooter();

function add_missing_null($value) {
    if ($value < 10) return "0".$value;
    else return $value;
}

function pulldowncode($name,$startval,$endval,$interval,$selected=0) {
	echo "<select name=\"$name\" class=\"pulldown_code\">\n";
	for ($i=$startval;$i<=$endval;$i += $interval) {
		echo "<option value=\"$i\"";
		if ($selected == $i) echo "selected";
		echo ">$i</option>\n";
	}
	echo "</select>\n";
}
?>
